import request from "@/utils/request";

class UserAPI {
  static getUsers(params?: UsersData) {
    return request<any, PageResult<User[]>>({
      url: "/users",
      method: "get",
      params,
    });
  }
  static editUser(data: User) {
    return request<any, any>({
      url: "/users",
      method: "POST",
      data,
    });
  }

  static deleteUser(data: searchByIdData) {
    return request<any, any>({
      url: `/users/${data.id}`,
      method: "delete",
      data,
    });
  }
  static addUser(data: addUserData | User) {
    return request<any, any>({
      url: "/users",
      method: "put",
      data,
    });
  }
  static upload(file: File) {
    const form = new FormData();
    form.append("picture", file, file.name);
    return request<any, ResponseData<string>>({
      url: "/upload",
      method: "post",
      data: form,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  }
  static searchById(params: searchByIdData) {
    return request<any, User[]>({
      url: `/users/${params.id}`,
      method: "get",
    });
  }
}

export default UserAPI;

export interface UsersData {
  id?: number;
  userName?: string;
  realName?: string;
  gender?: 0 | 1 | 2;
  grade?: number;
}

export interface User extends PictureData, searchByIdData {
  userName: string;
  realName: string;
  password: string;
  gender: 0 | 1 | 2;
  grade: number;
  isAdmin: number;
}

export interface addUserData extends PictureData {
  userName: string;
  password: string;
  gender: 0 | 1 | 2;
}

export interface searchByIdData {
  id: number;
}

export interface PictureData {
  picture: string | Blob;
}
